package com.bhs.sansonglogistics.ui.mine;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.ArrayMap;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.LineListBean;
import com.bhs.sansonglogistics.bean.TabBean;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.PictureSelectAdapter;
import com.bhs.sansonglogistics.ui.wallet.AddSucceedActivity;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.bhs.sansonglogistics.view.AutoLineFeedLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 意见反馈
 */
public class FeedbackActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private TextView mTvRecord;
    private RecyclerView mRvFeedback;
    private BaseQuickAdapter<TabBean, BaseViewHolder> mAdapterFeedback;
    private List<TabBean> feedbackList = new ArrayList<>();
    private EditText mEtFeedback;
    private TextView mTvWordCount;
    private RecyclerView mRvPicture;
    private PictureSelectAdapter selectAdapter;
    private Button mBtnSubmit;
    private boolean isSelect;
    private String typeName;

    @Override
    public int initLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initView() {

        mTvRecord = findViewById(R.id.tv_record);
        mRvFeedback = findViewById(R.id.rv_feedback);
        mEtFeedback = findViewById(R.id.et_feedback);
        mTvWordCount = findViewById(R.id.tv_word_count);
        mRvPicture = findViewById(R.id.rv_picture);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(this);
        selectAdapter = new PictureSelectAdapter(3, this);
        mRvPicture.setAdapter(selectAdapter);
        mRvPicture.setLayoutManager(new GridLayoutManager(this, 3));
        mTvRecord.setOnClickListener(this);
    }

    @Override
    public void initData() {
        feedbackList.add(new TabBean("新功能建议"));
        feedbackList.add(new TabBean("功能问题"));
        feedbackList.add(new TabBean("平台规则"));
        feedbackList.add(new TabBean("其他"));
        mAdapterFeedback = new BaseQuickAdapter<TabBean, BaseViewHolder>(R.layout.item_line, feedbackList) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, TabBean item) {
                TextView tvName = helper.getView(R.id.tv_name);
                tvName.setText(item.getTypeName());
                tvName.setBackgroundResource(item.isChecked() ? R.drawable.btn_vehicle : R.drawable.border_black_4);
            }
        };
        mRvFeedback.setAdapter(mAdapterFeedback);
        mRvFeedback.setLayoutManager(new AutoLineFeedLayoutManager());
        mAdapterFeedback.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                isSelect = true;
                for (TabBean datum : mAdapterFeedback.getData()) {
                    datum.setChecked(false);
                }
                typeName = mAdapterFeedback.getData().get(position).getTypeName();
                mAdapterFeedback.getData().get(position).setChecked(true);
                mAdapterFeedback.notifyDataSetChanged();
                verify();
            }
        });
        mEtFeedback.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mTvWordCount.setText(String.format("%s/300", editable.length()));
                verify();
            }
        });
    }

    private void verify() {
        boolean verify = true;
        if (TextUtils.isEmpty(mEtFeedback.getText())) {
            verify = false;
        }
        if (!isSelect) {
            verify = false;
        }

        mBtnSubmit.setEnabled(verify);
        mBtnSubmit.setBackgroundColor(verify ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.tv_record) {
            startActivity(new Intent(mActivity, FeedbackRecordingActivity.class));
        } else {
            loadingPopupShow();
            Map<String, String> map = new ArrayMap<>();
            map.put("platform", "logistics");
            map.put("type", typeName);
            map.put("content", mEtFeedback.getText().toString());
            map.put("imgs", selectAdapter.getImages());
            networkRequest(netApi.advice_report(map), this);
        }
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        loadingPopupDismiss();
        if (bean.getCode().equals("1")) {
            startActivity(new Intent(mActivity, AddSucceedActivity.class));
            ToastUtil.show("提交成功");
            finish();
        } else {
            ToastUtil.show(bean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        loadingPopupDismiss();
    }
}